from invenio.config import CFG_SITE_URL
from invenio.bibformat_engine import BibFormatObject
import os

def format_element(bfo):
	"""
	Includes all videos (MP4s) in Flowplayer
	"""
	formats = [".mp4"]
	fulltexts = bfo.fields("8564_")
	videos = []
	
	
	for fulltext in fulltexts:
		link = fulltext.get("u", "")
		if os.path.splitext(link)[1] in formats:
			videos.append(link)
			
		
	if videos:
		playerpath = CFG_SITE_URL + "/img/flowplayer"
		res = '<script type="text/javascript" src="%s/flowplayer-3.2.4.min.js"></script>\n' % playerpath
		for video in videos:
			filename = os.path.splitext(video.split("/")[-1])[0]
			res += '<a href="%s" style="display:block;width:520px;height:330px" id="%s"></a>\n' % (video, filename)
			res += """<script>flowplayer("%s", 
										 {src : "%s/flowplayer-3.2.5.swf",
										  wmode : "opaque"}
										  ,
										{plugins:{
											controls:{url: "%s/flowplayer.controls-3.2.3.swf"}
										},
										clip:{
											autoPlay : false
										}
										
										
										});
						 
						 </script><br />""" % (filename, playerpath, playerpath)
		return res
		
def escape_values(bfo):
    """
    Called by BibFormat in order to check if output of this element
    should be escaped.
    """
    return 0

	
